package com.wlyuan.index.application.translator;

import com.wlyuan.index.api.vo.WaybillIndexScrollVO;
import com.wlyuan.index.api.vo.WaybillIndexVO;
import com.wlyuan.index.domain.index.WaybillIndex;
import com.wlyuan.index.domain.index.WaybillIndexScroll;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;
import java.util.Optional;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class WaybillIndexTranslator {
    @Mapper
    public interface Translator {
        WaybillIndexVO translate(WaybillIndex source);

        default BigDecimal format(BigDecimal volume) {
            return Optional.ofNullable(volume)
                    .map(BigDecimal::stripTrailingZeros)
                    .map(BigDecimal::toPlainString)
                    .map(BigDecimal::new)
                    .orElse(null);
        }

        WaybillIndexScrollVO translate(WaybillIndexScroll source);
    }

    private static final Translator TRANSLATOR = Mappers.getMapper(Translator.class);

    public static WaybillIndexVO translate(WaybillIndex source) {
        return TRANSLATOR.translate(source);
    }

    public static WaybillIndexScrollVO translate(WaybillIndexScroll source) {
        return TRANSLATOR.translate(source);
    }
}
